package com.smzd.utils.email;

import java.util.Map;

import org.springframework.beans.factory.InitializingBean;

import com.smzd.utils.freemarker.FreeMarkerHelper;

public class EmailHelper implements InitializingBean {
	private EmailConfig emailConfig;
	private FreeMarkerHelper freeMarkerHelper;

	public FreeMarkerHelper getFreeMarkerHelper() {
		return freeMarkerHelper;
	}

	public void setFreeMarkerHelper(FreeMarkerHelper freeMarkerHelper) {
		this.freeMarkerHelper = freeMarkerHelper;
	}

	public EmailHelper() {
	}

	public EmailHelper(EmailConfig emailConfig) {
		this.emailConfig = emailConfig;
	}

	public EmailConfig getEmailConfig() {
		return emailConfig;
	}

	public void setEmailConfig(EmailConfig emailConfig) {
		this.emailConfig = emailConfig;
	}

	public void sendEmailByView(Map<String, ?> root, String to, String subject,
			String content) throws Exception {
		sendEmailByView(root, to, subject, content, null, null);
	}

	public void sendEmailByView(Map<String, ?> root, String to, String subject,
			String content, Map<String, String> pictures,
			Map<String, String> attachments) throws Exception {
		to = freeMarkerHelper.renderByView(to, root);
		subject = freeMarkerHelper.renderByView(subject, root);
		content = freeMarkerHelper.renderByView(content, root);
		sendEmail(to, subject, content, pictures, attachments);
	}

	public void sendEmailByViewWithPictures(Map<String, ?> root, String to,
			String subject, String content, Map<String, String> pictures)
			throws Exception {
		sendEmailByView(root, to, subject, content, pictures, null);
	}

	public void sendEmailByViewWithAttachments(Map<String, ?> root, String to,
			String subject, String content, Map<String, String> attachments)
			throws Exception {
		sendEmailByView(root, to, subject, content, null, attachments);
	}

	public void sendEmail(Map<String, ?> root, String to, String subject,
			String content) throws Exception {
		sendEmail(root, to, subject, content, null, null);
	}

	public void sendEmail(Map<String, ?> root, String to, String subject,
			String content, Map<String, String> pictures,
			Map<String, String> attachments) throws Exception {
		to = FreeMarkerHelper.render(to, root);
		subject = FreeMarkerHelper.render(subject, root);
		content = FreeMarkerHelper.render(content, root);
		sendEmail(to, subject, content, pictures, attachments);
	}

	public void sendEmailWithPictures(Map<String, ?> root, String to,
			String subject, String content, Map<String, String> pictures)
			throws Exception {
		sendEmail(root, to, subject, content, pictures, null);
	}

	public void sendEmailWithAttachments(Map<String, ?> root, String to,
			String subject, String content, Map<String, String> attachments)
			throws Exception {
		sendEmail(root, to, subject, content, null, attachments);
	}

	public void sendEmail(String to, String subject, String content)
			throws Exception {
		sendEmail(to, subject, content, null, null);
	}

	public void sendEmail(String to, String subject, String content,
			Map<String, String> pictures, Map<String, String> attachments)
			throws Exception {
		Email email = new Email(emailConfig.getHost(), emailConfig.getFrom(),
				emailConfig.getUsername(), emailConfig.getPassword(), to,
				subject, content, pictures, attachments);
		EmailUtils.sendEmail(email);
	}

	public void sendEmailWithPictures(String to, String subject,
			String content, Map<String, String> pictures) throws Exception {
		sendEmail(to, subject, content, pictures, null);
	}

	public void sendEmailWithAttachments(String to, String subject,
			String content, Map<String, String> attachments) throws Exception {
		sendEmail(to, subject, content, null, attachments);
	}

	
	private static EmailHelper instance;
	public static EmailHelper getInstance(){
		return instance;
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		instance = this;
	}
}
